<template>
  <div>
    <div class="info-box">
      <van-image
        width="62"
        height="62"
        round
        fit="cover"
        :src="userInfo.photo"
      />
      <h3 class="name">
        {{ userInfo.name }}<br />
        <van-tag plain type="primary">{{ userInfo.birthday }}</van-tag>
      </h3>
    </div>
    <div class="user-nav">
      <van-row>
        <van-col span="8">
          <van-icon name="newspaper-o" color="#7af" />我的作品
        </van-col>
        <van-col span="8">
          <van-icon name="newspaper-o" color="#7af" />我的收藏
        </van-col>
        <van-col span="8">
          <van-icon name="newspaper-o" color="#7af" />阅读历史
        </van-col>
      </van-row>
    </div>
    <div>
      <van-cell-group>
        <van-cell icon="edit" title="编辑资料" is-link />
        <van-cell icon="chat-o" title="小凡同学" is-link />
        <van-cell icon="setting-o" title="系统设置" is-link />
        <van-cell
          icon="warning-o"
          title="退出登录"
          is-link
          @click="logoutClick"
        />
      </van-cell-group>
    </div>
  </div>
</template>

<script>
//导入网络中间层
import { getUserInfo } from "./user.js";

//导入token。js包，退出删除token操作
import { removeToken } from '../../utils/token.js'

export default {
  name: "user",
  data() {
    return {
      //1.添加一个空数据对象
      userInfo: {},
    };
  },
  //2.生命周期勾子 created 发送请求
  created() {
    getUserInfo().then((res) => {
      console.log(res);
      //3.绑定数据渲染个人信息
      this.userInfo = res.data.data;
    });
  },
  methods: {
    logoutClick() {
      this.$dialog.confirm({
        title: "退出登录",
        message: "您真的要退出登录了吗?",
        theme: 'round-button'
      }).then(() => {
        // on confirm
        //1.删除token值
        removeToken()
        //2.清空用户信息
        this.userInfo={}
        //3.跳转到登录页面
        this.$router.push( { path:'/login' } )
      }).catch(() => {
    // on cancel
    //取消登录，不用操作
  })
    },
  },
};
</script>

<style lang="less">
.info-box {
  background-color: pink;
  height: 100px;
  display: flex;
  align-items: center;
  padding-left: 15px;

  .name {
    font-size: 18px;
    margin-left: 15px;
    color: white;
    font-weight: normal;
  }
}

/* 操作导航 */
.user-nav {
  padding: 15px 0;
  font-size: 12px;
  text-align: center;
  background-color: #fff;
  .van-icon {
    display: block;
    font-size: 24px;
    padding-bottom: 5px;
  }
}
</style>